/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.account.service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.account.domain.AccountAc50;
import me.zhengjie.modules.account.domain.AccountAc50a5;
import me.zhengjie.modules.account.domain.dto.AccountAc08QueryCriteria;
import me.zhengjie.modules.account.mapper.AccountAc50a5Mapper;
import me.zhengjie.modules.account.mapper.AccountCommonMapper;
import me.zhengjie.modules.account.service.AccountAc50a5Service;
import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.tool.ListUtil;
import me.zhengjie.tool.NPETool;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;

import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.YHConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author lzw
* @date 2025-06-09
**/
@Service
@RequiredArgsConstructor
public class AccountAc50a5ServiceImpl extends ServiceImpl<AccountAc50a5Mapper, AccountAc50a5> implements AccountAc50a5Service {

    private final AccountAc50a5Mapper accountAc50a5Mapper;
    private final AccountCommonMapper accountCommonMapper;
    private final SequenceService sequenceService;
    private final BusinessLogService businessLogService;

    @Override
    public List<AccountAc50a5> query(Map map) {
        QueryWrapper<AccountAc50a5> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aaz159",map.get("aaz159"));
        return accountAc50a5Mapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(AccountAc50a5 resources) throws IOException {
        check(resources);
        accountAc50a5Mapper.insert(resources);

        //保存日志
        saveLog(resources,YHConstants.AAA121_020101);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AccountAc50a5 resources) throws IOException {

        UpdateWrapper<AccountAc50a5> ac50a5 = new UpdateWrapper<>();
        ac50a5.eq("aaz159",resources.getAaz159());
        ac50a5.eq("aae001",resources.getAae001());
        ac50a5.set("aae001",resources.getAae001());
        ac50a5.set("bae095",resources.getBae095());
        update(ac50a5);

        //保存日志
        saveLog(resources,YHConstants.AAA121_020102);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<AccountAc50a5> ac50a5s) throws IOException {
        QueryWrapper<AccountAc50a5> queryWrapper = new QueryWrapper<>();
        List<String> aaz159s = CollUtil.map(ac50a5s, AccountAc50a5::getAaz159,true);
        List<BigDecimal> aae001s = CollUtil.map(ac50a5s, AccountAc50a5::getAae001, true);
        queryWrapper.in("aaz159",aaz159s);
        queryWrapper.in("aae001",aae001s);
        remove(queryWrapper);

        //保存日志
        saveLog(ac50a5s.get(0),YHConstants.AAA121_020103);

    }

    @Override
    public void download(List<AccountAc50a5> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AccountAc50a5 accountAc50a5 : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("年度", accountAc50a5.getAae001());
            map.put("年度月数", accountAc50a5.getBae095());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

   public void check(AccountAc50a5 businessMap){
       NPETool.isEmptyThrowException(businessMap.getAaz159(),"人员参保关系ID");
       NPETool.isEmptyThrowException(businessMap.getAae001(),"年度");
       NPETool.isEmptyThrowException(businessMap.getBae095(),"年度月数");

       Integer bae095 = Integer.parseInt(businessMap.getBae095().toString());

       if(bae095>=24||bae095<=0){
           throw new IllegalArgumentException("年度月数必须大于0小于24");
       }
       QueryWrapper<AccountAc50a5> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("aaz159",businessMap.getAaz159());
       queryWrapper.eq("aae001",businessMap.getAae001());
       List<AccountAc50a5> accountAc50a5s = accountAc50a5Mapper.selectList(queryWrapper);
       if(accountAc50a5s.size()>0){
           throw new IllegalArgumentException("当前年度已经存在业务年度信息");
       }


   }


    public void saveLog(AccountAc50a5 resources,String aaa121) throws IOException {
        String yyyyMMddHHmmss = DateUtil.getCurrentTime("yyyyMMddHHmmss");

        //写日志
        Map map= new HashMap<>();

        //查询人员基本信息
        Map q = new HashMap();
        q.put("aaz159", resources.getAaz159());
        List<Map> list1 = accountCommonMapper.queryAc01Ac02(q);



        map.put("aaz002",sequenceService.nextSequence(new Sequence("aaz002")));
        map.put("aaa121", aaa121);
        map.put("aab001","");
        map.put("aab004","");
        map.put("aac001",list1.get(0).get("aac001"));
        map.put("aac002",list1.get(0).get("aac002"));
        map.put("aac003",list1.get(0).get("aac003"));
        map.put("aae140", Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        map.put("aae016",Constant.AAE016_1_SHTG);
        map.put("aae011", SecurityUtils.getCurrentUsername());
        map.put("aae036",yyyyMMddHHmmss);
        map.put("aae014",SecurityUtils.getCurrentUsername());
        map.put("aae015",yyyyMMddHHmmss);
        map.put("aab359","530111");

        //记录日志信息
        BusinessLog businessLog = BusinessLog.singleLog(map);
        businessLogService.save(businessLog);

    }
}
